package com.chao.deeply.aqs;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.AbstractQueuedSynchronizer;

/**
 * @author : Rookie
 * @description :
 * @date : 2023/4/26
 */
public class CountDownLatchTest {

    private static int THREAD_COUNT = 10;

    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService = Executors.newCachedThreadPool();
        CountDownLatch latch = new CountDownLatch(THREAD_COUNT);

        for (int i = 0; i < THREAD_COUNT; i++) {
            int threadNum = i;
            executorService.execute(() -> {
                test(threadNum);
                System.out.println("线程" + threadNum + "执行完了");
                // 减一
                latch.countDown();
            });
        }

        latch.await();
        // 等上面线程执行完成 才会执行下面的代码
        System.out.println("main线程走完");
    }

    private static void test(int threadNum) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
